import pytz
import datetime
from python_baway.common.xcommon import sep, xeval


def xdt_change_tz(xdt, xdest_tz, xsrc_tz = None):
    if xsrc_tz is not None:
        src_dt = xsrc_tz.localize(xdt)
    else:
        src_dt = xdt
    xdest_tz = src_dt.astimezone(xdest_tz)
    return xdest_tz


ny_tz = pytz.timezone('America/New_York')
sh_tz = pytz.timezone('Asia/Shanghai')
cst_tz = datetime.timezone(datetime.timedelta(hours=8))

sep('Class relationships')
xcls = type(ny_tz)
print('xcls', repr(xcls))
xeval('issubclass(xcls, datetime.tzinfo)', xcls=xcls, datetime=datetime)
xeval('issubclass(xcls, datetime.timezone)', xcls=xcls, datetime=datetime)
xeval('issubclass(datetime.timezone, datetime.tzinfo)', datetime=datetime)

print('issubclass(xcls, datetime.tzinfo) =>', issubclass(xcls, datetime.tzinfo))
print('issubclass(xcls, datetime.timezone) =>', issubclass(xcls, datetime.timezone))
print('issubclass(datetime.timezone, datetime.tzinfo) =>', issubclass(datetime.timezone, datetime.tzinfo))

sep('now', 128)

sep('naive sh=>ny, now')
xnaive_dt = datetime.datetime.now()
xdest_dt = xdt_change_tz(xnaive_dt, ny_tz, sh_tz)
print(f'sh: {xnaive_dt} => ny: {xdest_dt}')

sep('0:00:00', 128)

sep('aware combination pytz.sh=>ny (bad)')
xtoday = datetime.date.today()
xsh_dt = datetime.datetime.combine(xtoday, datetime.time(0, 0, 0), sh_tz)
xdest_dt = xdt_change_tz(xsh_dt, ny_tz)
print(f'sh: {xsh_dt} {xsh_dt.tzname()} => ny: {xdest_dt} {xdest_dt.tzname()}')

sep('aware pytz.sh=>ny (bad)')
xsh_dt = datetime.datetime(xtoday.year, xtoday.month, xtoday.day, 0, 0, 0, 0, sh_tz)
xdest_dt = xdt_change_tz(xsh_dt, ny_tz)
print(f'sh: {xsh_dt.isoformat()} {xsh_dt.tzname()} => ny: {xdest_dt} {xdest_dt.tzname()}')

sep('aware tzinfo.sh=>ny')
xsh_dt = datetime.datetime(xtoday.year, xtoday.month, xtoday.day, 0, 0, 0, 0, cst_tz)
xdest_dt = xdt_change_tz(xsh_dt, ny_tz)
print(f'sh: {xsh_dt.isoformat()} {xsh_dt.tzname()} => ny: {xdest_dt} {xdest_dt.tzname()}')

sep('naive sh=>ny, 0:0')
xnaive_dt = datetime.datetime(xtoday.year, xtoday.month, xtoday.day, 0, 0, 0, 0)
xdest_dt = xdt_change_tz(xnaive_dt, ny_tz, sh_tz)
print(f'sh: {xnaive_dt} {xnaive_dt.tzname()} => ny: {xdest_dt} {xdest_dt.tzname()}')
